<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>C++ STL 六组件介绍 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言C++ STL 六组件介绍。  概念 C++：编程语言  STL（Standard Template Library，标准模板库）：C++ 标准库的一部分，提供相关工具（函数模板、类模板和函数等）    “函数模板” 和 “类模板” 是“抽象”概念，“模板函数” 和 “模板类” 是“具体”概念。   六组件：容器，算法，迭代器，适配器，仿函数，分配器  基础（常用） 三组件：容器，算法，迭代器">
<meta property="og:type" content="article">
<meta property="og:title" content="C++ STL 六组件介绍">
<meta property="og:url" content="http://example.com/2023/11/03/C++%20STL%20%E5%85%AD%E7%BB%84%E4%BB%B6%E4%BB%8B%E7%BB%8D/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言C++ STL 六组件介绍。  概念 C++：编程语言  STL（Standard Template Library，标准模板库）：C++ 标准库的一部分，提供相关工具（函数模板、类模板和函数等）    “函数模板” 和 “类模板” 是“抽象”概念，“模板函数” 和 “模板类” 是“具体”概念。   六组件：容器，算法，迭代器，适配器，仿函数，分配器  基础（常用） 三组件：容器，算法，迭代器">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover12.png">
<meta property="article:published_time" content="2023-11-03T04:23:20.000Z">
<meta property="article:modified_time" content="2024-02-28T03:07:27.150Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover12.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2023/11/03/C++%20STL%20%E5%85%AD%E7%BB%84%E4%BB%B6%E4%BB%8B%E7%BB%8D/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'C++ STL 六组件介绍',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-28 11:07:27'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover12.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">C++ STL 六组件介绍</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-11-03T04:23:20.000Z" title="发表于 2023-11-03 12:23:20">2023-11-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-28T03:07:27.150Z" title="更新于 2024-02-28 11:07:27">2024-02-28</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/C/">C++</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="C++ STL 六组件介绍"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>C++ STL 六组件介绍。</p>
<hr>
<h1 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h1><ul>
<li><p>C++：编程语言</p>
</li>
<li><p>STL（Standard Template Library，标准模板库）：C++ 标准库的一部分，提供相关工具（<strong>函数模板</strong>、<strong>类模板</strong>和函数等）</p>
</li>
</ul>
<blockquote>
<p><strong>“函数模板”</strong> 和 <strong>“类模板”</strong> 是“抽象”概念，<strong>“模板函数”</strong> 和 <strong>“模板类”</strong> 是“具体”概念。</p>
</blockquote>
<ul>
<li><p>六组件：容器，算法，迭代器，适配器，仿函数，分配器</p>
</li>
<li><p><strong>基础（常用）</strong> 三组件：容器，算法，迭代器</p>
</li>
<li><p><strong>拓展（少用）</strong> 三组件：适配器，仿函数，分配器</p>
</li>
</ul>
<hr>
<h1 id="容器（container）"><a href="#容器（container）" class="headerlink" title="容器（container）"></a>容器（container）</h1><h2 id="概念-1"><a href="#概念-1" class="headerlink" title="概念"></a>概念</h2><ul>
<li>组织数据的结构（所以称作：容纳物体的器具 -&gt; 容器）</li>
<li>特性：静态</li>
<li>类比：数组</li>
</ul>
<h2 id="类型"><a href="#类型" class="headerlink" title="类型"></a>类型</h2><p>顺序容器</p>
<ul>
<li>数组&#x2F;向量</li>
<li>链表</li>
<li>队列</li>
<li>栈</li>
</ul>
<p>关联容器</p>
<ul>
<li>图&#x2F;映射</li>
<li>集合</li>
<li>对组</li>
<li>元组</li>
</ul>
<p>数组&#x2F;向量</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>array</td>
<td>静态数组</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>vector</td>
<td>动态数组</td>
<td></td>
</tr>
</tbody></table>
<p> 链表</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>list</td>
<td>双向链表</td>
<td></td>
</tr>
<tr>
<td>forward_list</td>
<td>单向链表</td>
<td>C++11 标准</td>
</tr>
</tbody></table>
<p> 队列</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>deque</td>
<td>管理数组+多个被管理数组</td>
<td></td>
</tr>
<tr>
<td>queue</td>
<td>deque</td>
<td>容器适配器</td>
</tr>
<tr>
<td>priority_queue</td>
<td>vector；堆（完全二叉树）</td>
<td>容器适配器</td>
</tr>
</tbody></table>
<p>栈</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>stack</td>
<td>deque</td>
<td>容器适配器</td>
</tr>
</tbody></table>
<p> 图&#x2F;映射</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>map</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>multimap</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>unordered_map</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>unordered_multimap</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
</tbody></table>
<p>集合</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>set</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>multiset</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>unordered_set</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>unordered_multiset</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
</tbody></table>
<p>对组</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>pair</td>
<td>结构体struct</td>
<td></td>
</tr>
</tbody></table>
<p>元组</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>tuple</td>
<td>递归继承类</td>
<td>C++11 标准</td>
</tr>
</tbody></table>
<p> 总表</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>底层原理</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>array</td>
<td>静态数组</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>vector</td>
<td>动态数组</td>
<td></td>
</tr>
<tr>
<td>list</td>
<td>双向链表</td>
<td></td>
</tr>
<tr>
<td>forward_list</td>
<td>单向链表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>deque</td>
<td>管理数组+多个被管理数组</td>
<td></td>
</tr>
<tr>
<td>queue</td>
<td>deque</td>
<td>容器适配器</td>
</tr>
<tr>
<td>priority_queue</td>
<td>vector；堆（完全二叉树）</td>
<td></td>
</tr>
<tr>
<td>stack</td>
<td>deque</td>
<td>容器适配器</td>
</tr>
<tr>
<td>map</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>multimap</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>unordered_map</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>unordered_multimap</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>set</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>multiset</td>
<td>红黑树</td>
<td></td>
</tr>
<tr>
<td>unordered_set</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>unordered_multiset</td>
<td>哈希表</td>
<td>C++11 标准</td>
</tr>
<tr>
<td>pair</td>
<td>结构体 struct</td>
<td></td>
</tr>
<tr>
<td>tuple</td>
<td>递归继承类</td>
<td>C++11 标准</td>
</tr>
</tbody></table>
<hr>
<h2 id="代码示例"><a href="#代码示例" class="headerlink" title="代码示例"></a>代码示例</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span> <span class="comment">// vector&lt;&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 容器</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;; <span class="comment">// 存储“0、1和2”3个数据</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; vec.<span class="built_in">at</span>(<span class="number">0</span>) &lt;&lt; endl; <span class="comment">// 访问索引“0”的数据“0”</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 输出：0</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="作用（为什么需要）"><a href="#作用（为什么需要）" class="headerlink" title="作用（为什么需要）"></a>作用（为什么需要）</h2><ul>
<li>可以高效地组织数据</li>
<li>如：直接使用 vector&lt;&gt; 存储数据，而无需自己编写与数组类似并比数组“高级”的“顺序表”数据结构</li>
<li>如：直接使用 list&lt;&gt; 存储数据，而无需自己编写“双向链表”数据结构</li>
</ul>
<hr>
<h1 id="算法（algorithm）"><a href="#算法（algorithm）" class="headerlink" title="算法（algorithm）"></a>算法（algorithm）</h1><h2 id="概念-2"><a href="#概念-2" class="headerlink" title="概念"></a>概念</h2><ul>
<li>处理数据的操作（所以称作：算术的法门 -&gt; 算法）</li>
<li>特性：动态</li>
<li>类比：函数</li>
</ul>
<h2 id="类型-1"><a href="#类型-1" class="headerlink" title="类型"></a>类型</h2><blockquote>
<p>这里只列举部分算法，更多参见：<a target="_blank" rel="noopener" href="https://zh.cppreference.com/w/cpp/algorithm">算法库 - cppreference.com</a></p>
</blockquote>
<p>不修改序列的操作</p>
<ul>
<li>count()</li>
<li>find()</li>
</ul>
<p>修改序列的操作</p>
<ul>
<li>copy()</li>
<li>swap()</li>
</ul>
<p> 划分操作</p>
<ul>
<li>partition()</li>
<li>stable_partition()</li>
</ul>
<p>排序操作</p>
<ul>
<li>sort()</li>
<li>stable_sort()</li>
</ul>
<p>二分搜索操作（在已排序范围上）</p>
<ul>
<li>lower_bound()</li>
<li>binary_search()</li>
</ul>
<p>其他已排序范围上的操作</p>
<ul>
<li>merge()</li>
<li>inplace_merge()</li>
</ul>
<p>集合操作（在已排序范围上）</p>
<ul>
<li>includes()</li>
<li>set_difference()</li>
</ul>
<p>堆操作</p>
<ul>
<li>is_heap()</li>
<li>sort_heap()</li>
</ul>
<p>最小&#x2F;最大操作</p>
<ul>
<li>max()</li>
<li>min()</li>
</ul>
<p>比较操作</p>
<ul>
<li>equal()</li>
<li>lexicographical_compare()</li>
</ul>
<p>排列操作</p>
<ul>
<li>is_permutation()</li>
<li>next_permutation()</li>
</ul>
<p>数值运算</p>
<ul>
<li>iota()</li>
<li>accumulate()</li>
</ul>
<p>未初始化内存上的操作</p>
<ul>
<li>destroy_at()</li>
<li>construct_at()</li>
</ul>
<p>C 库</p>
<ul>
<li>qsort()</li>
<li>bsearch()</li>
</ul>
<hr>
<h2 id="代码示例-1"><a href="#代码示例-1" class="headerlink" title="代码示例"></a>代码示例</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="comment">// #include &lt;algorithm&gt;	// max()</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> num1 = <span class="number">1</span>;</span><br><span class="line">    <span class="type">int</span> num2 = <span class="number">2</span>;</span><br><span class="line">    </span><br><span class="line">	<span class="comment">// 算法</span></span><br><span class="line">    cout &lt;&lt; std::<span class="built_in">max</span>(num1, num2) &lt;&lt; endl; <span class="comment">// 获取最大值</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 输出：2</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="作用（为什么需要）-1"><a href="#作用（为什么需要）-1" class="headerlink" title="作用（为什么需要）"></a>作用（为什么需要）</h2><ul>
<li>可以高效地处理数据</li>
<li>如：直接使用 max() 比较数据，而无需自己编写比较并返回数据的比较函数算法</li>
<li>如：直接使用 find() 查找数据，而无需自己编写遍历、比较并返回数据的查找函数算法</li>
</ul>
<hr>
<h1 id="迭代器（iterator）"><a href="#迭代器（iterator）" class="headerlink" title="迭代器（iterator）"></a>迭代器（iterator）</h1><h2 id="概念-3"><a href="#概念-3" class="headerlink" title="概念"></a>概念</h2><ul>
<li><strong>连接</strong>容器和算法</li>
<li><strong>代表</strong>容器组织的数据：无需关心数据的内部实现细节，以统一的方式表示数据</li>
<li><strong>通过</strong>算法处理数据：操作数据时，以“逐一访问”&#x2F;遍历的表现形式（所以称作：迭代物品的器具 -&gt; 迭代器）</li>
<li>特性：静 -&gt; 动态</li>
<li>类比：数组下标&#x2F;索引&#x2F;位置&#x2F;指针&#x2F;地址</li>
</ul>
<h2 id="常用迭代器"><a href="#常用迭代器" class="headerlink" title="常用迭代器"></a>常用迭代器</h2><ul>
<li>正向迭代器：iterator&lt;&gt;</li>
<li>常量正向迭代器：const_iterator&lt;&gt;</li>
<li>反向迭代器：reverse_iterator&lt;&gt;</li>
<li>常量反向迭代器：const_reverse_iterator&lt;&gt;</li>
</ul>
<h2 id="类型和代码示例"><a href="#类型和代码示例" class="headerlink" title="类型和代码示例"></a>类型和代码示例</h2><p>类型</p>
<ul>
<li>输入迭代器</li>
<li>输出迭代器</li>
<li>前向迭代器</li>
<li>双向迭代器</li>
<li>随机访问迭代器</li>
<li>连续迭代器</li>
</ul>
<p>输入迭代器</p>
<ul>
<li>从<strong>输入流</strong>中<strong>逐个</strong>读出数据</li>
<li>支持单次遍历</li>
<li>istream_iterator&lt;&gt;</li>
</ul>
<blockquote>
<p>将“流”看作是一种容器，迭代器可以作用于它。</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iterator&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cin;</span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::istream_iterator;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 输入迭代器</span></span><br><span class="line">    <span class="function">istream_iterator&lt;<span class="type">int</span>&gt; <span class="title">input_it</span><span class="params">(cin)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> num = *input_it; <span class="comment">// 注意：“*”解引用操作就是输入操作</span></span><br><span class="line">    cout &lt;&lt; num &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    ++input_it; <span class="comment">// 迭代器需要“前进”偏移，否则会指向同一个“位置”</span></span><br><span class="line">    num = *input_it;</span><br><span class="line">    cout &lt;&lt; num &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输入：123</span></span><br><span class="line"><span class="comment">// 输出：123</span></span><br><span class="line"><span class="comment">// 输入：456</span></span><br><span class="line"><span class="comment">// 输出：456</span></span><br></pre></td></tr></table></figure>

<p>输出迭代器</p>
<ul>
<li>向<strong>输出流</strong>中<strong>逐个</strong>写入数据</li>
<li>支持单次遍历</li>
<li>ostream_iterator&lt;&gt;</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iterator&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::ostream_iterator;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 输出迭代器</span></span><br><span class="line">    <span class="comment">// ostream_iterator&lt;int&gt; output_it(cout);</span></span><br><span class="line">    <span class="function">ostream_iterator&lt;<span class="type">int</span>&gt; <span class="title">output_it</span><span class="params">(cout, <span class="string">&quot; &quot;</span>)</span></span>;	<span class="comment">// 第二个参数：每数据的分隔符</span></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> num = <span class="number">123</span>;</span><br><span class="line">    *output_it = num; <span class="comment">// 注意：“*”解引用操作就是输出操作</span></span><br><span class="line"></span><br><span class="line">    num = <span class="number">456</span>;</span><br><span class="line">    *output_it = num;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：123 456</span></span><br></pre></td></tr></table></figure>
<p>前向迭代器</p>
<ul>
<li>支持<strong>逐个前进</strong>，不支持<strong>逐个后退</strong></li>
</ul>
<blockquote>
<p>“前进”指“增加”即“++&#x2F;+1”操作，“后退”指“减少”即“–&#x2F;-1”操作。</p>
</blockquote>
<ul>
<li>支持多次遍历</li>
<li><strong>没有特定的类模板， 是 符合前向迭代器特性的 容器 对应的 迭代器的 类型</strong>（如 forward_list&lt;&gt; ，其底层原理是单向链表，支持单向前进顺序访问，不支持反向后退顺序访问，不支持随机访问，是 符合前向迭代器特性的 容器，其对应的 迭代器的 类型 是前向迭代器）</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;forward_list&gt;</span></span></span><br><span class="line"><span class="comment">// #include &lt;iterator&gt;</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::forward_list;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    forward_list&lt;<span class="type">int</span>&gt; for_list&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 前向迭代器</span></span><br><span class="line">    forward_list&lt;<span class="type">int</span>&gt;::const_iterator it_beg = for_list.<span class="built_in">begin</span>(); <span class="comment">// 数据“0”的“位置”</span></span><br><span class="line">    forward_list&lt;<span class="type">int</span>&gt;::const_iterator it_end = for_list.<span class="built_in">end</span>();   <span class="comment">// 数据“2”的后一个“位置”</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (forward_list&lt;<span class="type">int</span>&gt;::const_iterator it = it_beg; it != it_end; ++it)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; *it &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：0 1 2</span></span><br></pre></td></tr></table></figure>
<p>双向迭代器</p>
<ul>
<li><p>支持<strong>逐个前进</strong>，支持<strong>逐个后退</strong></p>
</li>
<li><p>支持多次遍历</p>
</li>
<li><p><strong>没有特定的类模板， 是 符合双向迭代器特性的 容器 对应的 迭代器的 类型</strong>（如 list&lt;&gt; ，其底层原理是双向链表，支持单向前进顺序访问，支持反向后退顺序访问，不支持随机访问，是 符合双向迭代器特性的 容器，其对应的 迭代器的 类型 是双向迭代器）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;list&gt;</span></span></span><br><span class="line"><span class="comment">// #include &lt;iterator&gt;</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::list;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    list&lt;<span class="type">int</span>&gt; li = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 双向迭代器</span></span><br><span class="line">    list&lt;<span class="type">int</span>&gt;::const_iterator it_beg = li.<span class="built_in">begin</span>(); <span class="comment">// 数据“0”的“位置”</span></span><br><span class="line">    list&lt;<span class="type">int</span>&gt;::const_iterator it_end = li.<span class="built_in">end</span>();   <span class="comment">// 数据“2”的后一个“位置”</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (list&lt;<span class="type">int</span>&gt;::const_iterator it = it_beg; it != it_end; ++it) <span class="comment">// 前进遍历</span></span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; *it &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    list&lt;<span class="type">int</span>&gt;::const_reverse_iterator it_rbeg = li.<span class="built_in">rbegin</span>(); <span class="comment">// 数据“2”的“位置”</span></span><br><span class="line">    list&lt;<span class="type">int</span>&gt;::const_reverse_iterator it_rend = li.<span class="built_in">rend</span>();   <span class="comment">// 数据“0”的前一个“位置”</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (list&lt;<span class="type">int</span>&gt;::const_reverse_iterator it = it_rbeg; it != it_rend; ++it) <span class="comment">// 后退遍历</span></span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; *it &lt;&lt; <span class="string">&quot; &quot;</span>; </span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：0 1 2</span></span><br><span class="line"><span class="comment">// 输出：2 1 0</span></span><br></pre></td></tr></table></figure>
<p>随机访问迭代器</p>
</li>
<li><p>支持<strong>任意前进和后退</strong></p>
</li>
<li><p>支持多次遍历</p>
</li>
<li><p><strong>没有特定的类模板， 是 符合随机访问迭代器特性的 容器 对应的 迭代器的 类型</strong>（如 vector&lt;&gt; ，其底层原理是动态数组，支持随机访问，是 符合随机访问迭代器特性的 容器，其对应的 迭代器的 类型 是随机访问迭代器）</p>
</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="comment">// #include &lt;iterator&gt;</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 随机访问迭代器</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::const_iterator it_beg = vec.<span class="built_in">begin</span>(); <span class="comment">// 数据“0”的“位置”</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; *(it_beg + <span class="number">2</span>) &lt;&lt; endl; <span class="comment">// 数据“2”的“位置”   随机访问</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：2</span></span><br></pre></td></tr></table></figure>
<p>连续迭代器</p>
<ul>
<li>C++17 标准引入</li>
<li>支持<strong>任意前进和后退</strong></li>
<li>支持多次遍历</li>
<li>与随机访问迭代器不同，连续迭代器保证数据在内存中是连续存储的，可以提供更高效的内存访问</li>
<li><strong>没有特定的类模板， 是 符合连续迭代器特性的 容器 对应的 迭代器的 类型</strong>（如 vector&lt;&gt; ，其底层原理是动态数组，支持随机访问，数据连续存储，是 符合连续迭代器特性的 容器，其对应的 迭代器的 类型 是连续迭代器）<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="comment">// #include &lt;iterator&gt;</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 连续迭代器</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::const_iterator it_beg = vec.<span class="built_in">begin</span>(); <span class="comment">// 数据“0”的“位置”</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; *(it_beg + <span class="number">2</span>) &lt;&lt; endl; <span class="comment">// 数据“2”的“位置”   随机访问</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：2</span></span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h2 id="容器对应的迭代器的类型"><a href="#容器对应的迭代器的类型" class="headerlink" title="容器对应的迭代器的类型"></a>容器对应的迭代器的类型</h2><p>数组&#x2F;向量</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>array</td>
<td>随机访问迭代器</td>
</tr>
<tr>
<td>vector</td>
<td>随机访问迭代器</td>
</tr>
</tbody></table>
<p>链表</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>list</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>forward_list</td>
<td>前向迭代器</td>
</tr>
</tbody></table>
<p>栈</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>stack</td>
<td>不支持迭代器</td>
</tr>
</tbody></table>
<p>队列</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>deque</td>
<td>随机访问迭代器</td>
</tr>
<tr>
<td>queue</td>
<td>不支持迭代器</td>
</tr>
<tr>
<td>priority_queue</td>
<td>不支持迭代器</td>
</tr>
</tbody></table>
<p>图&#x2F;映射</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>map</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>multimap</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>unordered_map</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>unordered_multimap</td>
<td>前向迭代器</td>
</tr>
</tbody></table>
<p>集合</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>set</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>multiset</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>unordered_set</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>unordered_multiset</td>
<td>前向迭代器</td>
</tr>
</tbody></table>
<p>对组</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>pair</td>
<td>不支持迭代器</td>
</tr>
</tbody></table>
<p>元组</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器类型</th>
</tr>
</thead>
<tbody><tr>
<td>tuple</td>
<td>不支持迭代器</td>
</tr>
</tbody></table>
<p>总表</p>
<table>
<thead>
<tr>
<th>容器</th>
<th>对应的迭代器的类型</th>
</tr>
</thead>
<tbody><tr>
<td>array</td>
<td>随机访问迭代器</td>
</tr>
<tr>
<td>vector</td>
<td>随机访问迭代器</td>
</tr>
<tr>
<td>list</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>forward_list</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>stack</td>
<td>不支持迭代器</td>
</tr>
<tr>
<td>deque</td>
<td>随机访问迭代器</td>
</tr>
<tr>
<td>queue</td>
<td>不支持迭代器</td>
</tr>
<tr>
<td>priority_queue</td>
<td>不支持迭代器</td>
</tr>
<tr>
<td>map</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>multimap</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>unordered_map</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>unordered_multimap</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>set</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>multiset</td>
<td>双向迭代器</td>
</tr>
<tr>
<td>unordered_set</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>unordered_multiset</td>
<td>前向迭代器</td>
</tr>
<tr>
<td>pair</td>
<td>不支持迭代器</td>
</tr>
<tr>
<td>tuple</td>
<td>不支持迭代器</td>
</tr>
</tbody></table>
<hr>
<h2 id="作用（为什么需要）-2"><a href="#作用（为什么需要）-2" class="headerlink" title="作用（为什么需要）"></a>作用（为什么需要）</h2><ul>
<li><strong>连接</strong>容器和算法</li>
<li><strong>代表</strong>容器组织的数据：无需关心数据的内部实现细节，以统一的方式表示数据</li>
<li><strong>通过</strong>算法处理数据：操作数据时，以“逐一访问”&#x2F;遍历的表现形式</li>
<li>类比：容器 -&gt; 数组，算法 -&gt; 遍历，迭代器 -&gt;数组下标。为了实现遍历，需要通过数组下标获取数组的数据 -&gt; 为了实现算法，需要通过迭代器获取容器的数据</li>
</ul>
<hr>
<h1 id="容器，算法，迭代器代码示例"><a href="#容器，算法，迭代器代码示例" class="headerlink" title="容器，算法，迭代器代码示例"></a>容器，算法，迭代器代码示例</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span>    <span class="comment">// vector&lt;&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span> <span class="comment">// find()</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 容器</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;; <span class="comment">// 有“0、1和2”3个数据</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 迭代器</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::const_iterator it_beg = vec.<span class="built_in">begin</span>(); <span class="comment">// 数据“0”的“位置”</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::const_iterator it_end = vec.<span class="built_in">end</span>();   <span class="comment">// 数据“2”的后一个“位置”</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::const_iterator it_res = vec.<span class="built_in">end</span>();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 算法</span></span><br><span class="line">    it_res = std::<span class="built_in">find</span>(it_beg, it_end, <span class="number">1</span>); <span class="comment">// 查找数据“1”</span></span><br><span class="line">    <span class="keyword">if</span> (it_res != it_end)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;查找到数据&quot;</span> &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;未查找到数据&quot;</span> &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 输出：查找到数据</span></span><br></pre></td></tr></table></figure>

<hr>
<h1 id="适配器（adapter）"><a href="#适配器（adapter）" class="headerlink" title="适配器（adapter）"></a>适配器（adapter）</h1><h2 id="概念-4"><a href="#概念-4" class="headerlink" title="概念"></a>概念</h2><ul>
<li><strong>封装已有目标</strong>（容器、迭代器和函数）<strong>，提供不同形式的接口</strong>（特性和函数等）</li>
<li>类比：电源适配器将 220V 的交流电转换成 20V 的直流电提供给笔记本电脑；设计模式的适配器模式</li>
</ul>
<h2 id="类型和代码示例-1"><a href="#类型和代码示例-1" class="headerlink" title="类型和代码示例"></a>类型和代码示例</h2><p>类型</p>
<ul>
<li>容器适配器</li>
<li>迭代器适配器 <strong>（少用）</strong></li>
<li>函数适配器 <strong>（少用）</strong></li>
</ul>
<p>容器适配器</p>
<ul>
<li>stack&lt;&gt;</li>
<li>queue&lt;&gt;</li>
<li>priority_queue&lt;&gt;</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stack&gt;</span> <span class="comment">// stack&lt;&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::stack;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 容器适配器</span></span><br><span class="line">    stack&lt;<span class="type">int</span>&gt; st&#123;&#125;;</span><br><span class="line"></span><br><span class="line">    st.<span class="built_in">push</span>(<span class="number">0</span>); <span class="comment">// 入栈</span></span><br><span class="line">    st.<span class="built_in">push</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; st.<span class="built_in">top</span>() &lt;&lt; endl; <span class="comment">// 获取栈顶数据</span></span><br><span class="line"></span><br><span class="line">    st.<span class="built_in">pop</span>(); <span class="comment">// 出栈</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; st.<span class="built_in">top</span>() &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：1</span></span><br><span class="line"><span class="comment">// 输出：0</span></span><br></pre></td></tr></table></figure>

<p>迭代器适配器</p>
<ul>
<li>反向迭代器：reverse_iterator&lt;&gt;</li>
<li>插入迭代器：back_insert_iterator&lt;&gt;，front_insert_iterator&lt;&gt;，insert_iterator&lt;&gt;</li>
<li>流迭代器：istream_iterator&lt;&gt;，ostream_iterator&lt;&gt;</li>
<li>流缓冲迭代器：istreambuf_iterator&lt;&gt;，ostreambuf_iterator&lt;&gt;</li>
<li>移动迭代器：move_iterator&lt;&gt;</li>
</ul>
<blockquote>
<p>注意：“迭代器”和“迭代器适配器”的类型中有重叠的类型。如：stream_iterator&lt;&gt;，从“输入”的角度， 是“迭代器”的“输入迭代器”类型；从“流”的角度，是“迭代器适配器”的“流迭代器”类型。</p>
</blockquote>
<blockquote>
<p>一般不区分容器和容器适配器，迭代器和迭代器适配器。</p>
</blockquote>
<blockquote>
<p>代码示例见“迭代器”内容。</p>
</blockquote>
<p><strong>函数（普通函数、类方法、 仿函数和 Lambda 表达式等）</strong> 适配器</p>
<blockquote>
<p>注意：很多函数适配器已不推荐&#x2F;过时&#x2F;弃用：bind1st()、std::bind2nd() 和 mem_fun() 等，请注意甄别</p>
</blockquote>
<ul>
<li>改变函数参数：bind()</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;functional&gt;</span> <span class="comment">// bind()，placeholders</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">add</span><span class="params">(<span class="type">int</span> num1, <span class="type">int</span> num2)</span> <span class="comment">// add()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1 + num2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 函数适配器</span></span><br><span class="line">    <span class="keyword">auto</span> add_bind = std::<span class="built_in">bind</span>(add, <span class="number">1</span>, std::placeholders::_1); <span class="comment">// 绑定 add() 的第一个参数为数据“1”</span></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> result = <span class="built_in">add_bind</span>(<span class="number">2</span>); <span class="comment">// 调用 add_bind(x)，实际上是调用 add(1, x)</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; result &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：3</span></span><br></pre></td></tr></table></figure>

<ul>
<li>封装函数：function&lt;&gt;</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;functional&gt;</span> <span class="comment">// function&lt;&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">add</span><span class="params">(<span class="type">int</span> num1, <span class="type">int</span> num2)</span> <span class="comment">// add()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1 + num2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 函数适配器</span></span><br><span class="line">    std::function&lt;<span class="type">int</span>(<span class="type">int</span>, <span class="type">int</span>)&gt; add_func = add; <span class="comment">// 封装 add()</span></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> result = <span class="built_in">add_func</span>(<span class="number">1</span>, <span class="number">2</span>); <span class="comment">// 调用 add_func(x, y)，实际上是调用 add(x, y)</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; result &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：3</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="作用（为什么需要）-3"><a href="#作用（为什么需要）-3" class="headerlink" title="作用（为什么需要）"></a>作用（为什么需要）</h2><p>适配器</p>
<ul>
<li>可以在<strong>已有目标</strong>（容器、迭代器和函数）的基础上<strong>提供不同形式的接口</strong>（特性和函数等），以满足特定场景需求</li>
<li>如：已有容器不满足栈数据结构的特性，封装双端队列 deque&lt;&gt; 容器形成 stack&lt;&gt; 容器适配器。直接使用 stack&lt;&gt; 存储数据，而无需自己编写“栈”数据结构</li>
<li>如：已有容器不满足队列数据结构的特性，封装双端队列 deque&lt;&gt; 容器形成 queue&lt;&gt; 容器适配器。直接使用 queue&lt;&gt; 存储数据，而无需自己编写“队列”数据结构</li>
</ul>
<hr>
<h1 id="仿函数（functor）"><a href="#仿函数（functor）" class="headerlink" title="仿函数（functor）"></a>仿函数（functor）</h1><h2 id="概念-5"><a href="#概念-5" class="headerlink" title="概念"></a>概念</h2><ul>
<li>重载调用运算符（小括号“()”）的<strong>类的对象</strong>。（所以又称：函数对象）</li>
<li>重载调用运算符（小括号“()”）的<strong>类的对象</strong> 使用调用运算符时，表现形式和普通函数调用一样（所以称作：仿造的函数 -&gt; 仿函数）</li>
<li>类比：函数指针；Lambda 表达式</li>
</ul>
<h2 id="代码示例-2"><a href="#代码示例-2" class="headerlink" title="代码示例"></a>代码示例</h2><p>一般使用</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 仿函数类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Add_functor</span> <span class="comment">// add()</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">int</span> num1, <span class="type">int</span> num2)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> num1 + num2;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 仿函数（对象）</span></span><br><span class="line">    Add_functor add_functor;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> result = <span class="built_in">add_functor</span>(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; result &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：3</span></span><br></pre></td></tr></table></figure>

<p>算法使用</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 仿函数类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Comp</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">inline</span> <span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> <span class="type">int</span> &amp;num1, <span class="type">const</span> <span class="type">int</span> &amp;num2)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> num1 &gt; num2; <span class="comment">// 左数比右数大则返回 true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; vec&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  仿函数（对象）</span></span><br><span class="line">    Comp comp;</span><br><span class="line"></span><br><span class="line">    std::<span class="built_in">sort</span>(vec.<span class="built_in">begin</span>(), vec.<span class="built_in">end</span>(), comp); <span class="comment">// std::sort() 默认从小到大排序，使用仿函数（对象）定制从大到小排序</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">const</span> <span class="type">int</span> &amp;v : vec)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; v &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：2 1 0</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="作用（为什么需要）-4"><a href="#作用（为什么需要）-4" class="headerlink" title="作用（为什么需要）"></a>作用（为什么需要）</h2><ul>
<li>可以代替<strong>函数指针</strong>（函数指针的签名复杂）</li>
</ul>
<blockquote>
<p>函数指针：指向函数的指针</p>
</blockquote>
<ul>
<li>可以通过类的特性封装更多信息，执行更多操作</li>
</ul>
<blockquote>
<p>参见：<a target="_blank" rel="noopener" href="https://blog.csdn.net/u014386899/article/details/108590804">为什么要用仿函数？_为什么要把函数写成仿函数-CSDN博客</a></p>
</blockquote>
<hr>
<h1 id="分配器（allocator）"><a href="#分配器（allocator）" class="headerlink" title="分配器（allocator）"></a>分配器（allocator）</h1><h2 id="概念-6"><a href="#概念-6" class="headerlink" title="概念"></a>概念</h2><ul>
<li>为<strong>容器</strong>提供<strong>自定义的内存分配和释放，对象构造和析构</strong>功能（所以称作：分配空间的器具 -&gt; 分配器）</li>
</ul>
<blockquote>
<p>一般类型（内置类型和自定义类型等）内存分配（在堆上的动态内存分配）<strong>不推荐</strong>使用分配器（难用），容器分配内存<strong>可以</strong>使用分配器（<strong>少用</strong>，有需求才用）</p>
</blockquote>
<blockquote>
<p><strong>分配器本质：</strong> 将 1. 内存分配和对象构造 和 2. 对象析构和内存释放 两个过程 拆分成 1. 内存分配 2. 对象构造 3. 对象析构 4. 内存释放 四个过程，通过定制内存管理策略提高灵活性和效率</p>
</blockquote>
<ul>
<li>类比：malloc() - free()；构造函数 - 析构函数；new - delete</li>
</ul>
<hr>
<h2 id="代码示例-3"><a href="#代码示例-3" class="headerlink" title="代码示例"></a>代码示例</h2><p>一般类型（内置类型和自定义类型等）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;memory&gt;</span> <span class="comment">// allocator&lt;&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::allocator;</span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 分配器</span></span><br><span class="line">    allocator&lt;<span class="type">int</span>&gt; allo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 内存分配</span></span><br><span class="line">    <span class="type">int</span> *int_ptr = allo.<span class="built_in">allocate</span>(<span class="number">1</span>); <span class="comment">// 分配1个原始的/未初始化的 int 类型数据的内存</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 对象构造</span></span><br><span class="line">    allo.<span class="built_in">construct</span>(int_ptr, <span class="number">3</span>); <span class="comment">// 构造对象数据为“3”</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; *int_ptr &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 对象析构</span></span><br><span class="line">    allo.<span class="built_in">destroy</span>(int_ptr); <span class="comment">// 析构对象数据</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 内存释放</span></span><br><span class="line">    allo.<span class="built_in">deallocate</span>(int_ptr, <span class="number">1</span>); <span class="comment">// 释放1个原始的/未初始化的 int 类型数据的内存</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出：3</span></span><br></pre></td></tr></table></figure>
<p>容器类型</p>
<ul>
<li><strong>大多数</strong>（vector&lt;&gt; 和 list&lt;&gt; 等）容器的实现中，形参有一个默认分配器，其设计得足够安全和高效，<strong>一般不需要定制，</strong> 容器自动调用该默认分配器管理空间</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vector&lt;&gt; 的声明</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">class</span> <span class="title class_">T</span>, <span class="keyword">class</span> <span class="title class_">Allocator</span> = std::allocator&lt;T&gt;&gt;</span><br><span class="line"><span class="keyword">class</span> vector;</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>有需求时需要自定义分配器</strong></li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="comment">// #include &lt;memory&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// （自定义）分配器</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CustomAllocator</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 容器的设计要求：分配器必须提供一个“value_type”的类型别名，以便容器使用分配器时知道数据的类型</span></span><br><span class="line">    <span class="keyword">using</span> value_type = T;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 构造</span></span><br><span class="line">    <span class="built_in">CustomAllocator</span>() <span class="keyword">noexcept</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 内存分配</span></span><br><span class="line">    <span class="comment">// 可能失败，不声明为 noexcept</span></span><br><span class="line">    <span class="function">T *<span class="title">allocate</span><span class="params">(std::<span class="type">size_t</span> n)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Allocating memory for &quot;</span> &lt;&lt; n &lt;&lt; <span class="string">&quot; objects.&quot;</span> &lt;&lt; std::endl; <span class="comment">// 定制化体现</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> std::allocator&lt;T&gt;&#123;&#125;.<span class="built_in">allocate</span>(n); <span class="comment">// 使用标准分配器分配内存</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 内存释放</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">deallocate</span><span class="params">(T *p, std::<span class="type">size_t</span> n)</span> <span class="keyword">noexcept</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Deallocating memory for &quot;</span> &lt;&lt; n &lt;&lt; <span class="string">&quot; objects.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">        std::allocator&lt;T&gt;&#123;&#125;.<span class="built_in">deallocate</span>(p, n); <span class="comment">// 使用标准分配器释放内存</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 对象构造</span></span><br><span class="line">    <span class="comment">// 可能失败，不声明为 noexcept</span></span><br><span class="line">    <span class="comment">// typename...：声名可变参数模板</span></span><br><span class="line">    <span class="comment">// Args：类型参数包的名称；包含零或多个类型参数；指的是类型</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// args：值参数包的名称；包含零或多个值参数；指的是值</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// Args：可变参数的类型</span></span><br><span class="line">    <span class="comment">// &amp;&amp;：（右值引用）保留传递给函数的 参数的 值的 特性（常量/引用左值/右值）</span></span><br><span class="line">    <span class="comment">// ...：折叠运算符（在参数左边）</span></span><br><span class="line">    <span class="comment">// args：可变参数的值</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// new：new 运算符：1. 内存分配 2. 对象构造</span></span><br><span class="line">    <span class="comment">// new：placement new 运算符：对象构造</span></span><br><span class="line">    <span class="comment">// placement new 运算符需要一个 void* 参数，以指定在哪个内存地址上构造对象</span></span><br><span class="line">    <span class="comment">// forward&lt;&gt;()：完美转发：1. 类型（Args） 2. 值的特性（&amp;&amp;） 3. 值（args）</span></span><br><span class="line">    <span class="comment">// ...：展开运算符（在参数右边）</span></span><br><span class="line">    <span class="comment">// (args...)：展开所有参数并作为 forward&lt;&gt;() 的参数 ×</span></span><br><span class="line">    <span class="comment">// (args)...：分别展开所有参数并分别作为 forward&lt;&gt;() 的参数 √</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 使用搭配</span></span><br><span class="line">    <span class="comment">// malloc() - free()</span></span><br><span class="line">    <span class="comment">// 构造 - 析构</span></span><br><span class="line">    <span class="comment">// new - delete</span></span><br><span class="line">    <span class="comment">// 内存分配 - placement new - 析构 - delete</span></span><br><span class="line">    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> U, <span class="keyword">typename</span>... Args&gt;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">construct</span><span class="params">(U *p, Args &amp;&amp;...args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Constructing object.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">new</span> ((<span class="type">void</span> *)p) <span class="built_in">U</span>(std::forward&lt;Args&gt;(args)...); <span class="comment">// 使用对象的构造函数构造对象</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 析构函数，用于在分配的内存上销毁对象</span></span><br><span class="line">    <span class="comment">// 没有 placement detele 运算符</span></span><br><span class="line">    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> U&gt;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">destroy</span><span class="params">(U *p)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Destroying object.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">        p-&gt;~<span class="built_in">U</span>(); <span class="comment">// 使用对象的析构函数析构对象</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// （自定义）分配器</span></span><br><span class="line">    std::vector&lt;<span class="type">int</span>, CustomAllocator&lt;<span class="type">int</span>&gt;&gt; vec;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 内存分配</span></span><br><span class="line">    <span class="comment">// 2. 对象构造</span></span><br><span class="line">    <span class="comment">// 3. 对象析构</span></span><br><span class="line">    <span class="comment">// 4. 内存释放</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">2</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        vec.<span class="built_in">push_back</span>(i);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 输出：</span></span><br><span class="line"><span class="comment">Allocating memory for 1 objects.</span></span><br><span class="line"><span class="comment">Constructing object.</span></span><br><span class="line"><span class="comment">Allocating memory for 2 objects.</span></span><br><span class="line"><span class="comment">Constructing object.</span></span><br><span class="line"><span class="comment">Constructing object.</span></span><br><span class="line"><span class="comment">Destroying object.</span></span><br><span class="line"><span class="comment">Deallocating memory for 1 objects.</span></span><br><span class="line"><span class="comment">Destroying object.</span></span><br><span class="line"><span class="comment">Destroying object.</span></span><br><span class="line"><span class="comment">Deallocating memory for 2 objects.</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/* 逐行分析：</span></span><br><span class="line"><span class="comment">1.1 第一个容器内存分配</span></span><br><span class="line"><span class="comment">1.2 第一个容器第一个对象构造</span></span><br><span class="line"><span class="comment">2.1 第二个容器内存分配（容器扩容）</span></span><br><span class="line"><span class="comment">2.2 第二个容器第一个对象构造</span></span><br><span class="line"><span class="comment">2.2 第二个容器第二个对象构造</span></span><br><span class="line"><span class="comment">1.3 第一个容器第一个对象析构</span></span><br><span class="line"><span class="comment">1.4 第一个容器内存释放</span></span><br><span class="line"><span class="comment">2.3 第二个容器第二个对象析构</span></span><br><span class="line"><span class="comment">2.3 第二个容器第一个对象析构</span></span><br><span class="line"><span class="comment">2.4 第二个容器内存释放</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="作用（为什么需要）-5"><a href="#作用（为什么需要）-5" class="headerlink" title="作用（为什么需要）"></a>作用（为什么需要）</h2><ul>
<li>可以为<strong>容器</strong>提供<strong>自定义的内存分配和释放，对象构造和析构</strong>功能</li>
</ul>
<blockquote>
<p><strong>分配器本质：</strong> 将 1. 内存分配和对象构造 和 2. 对象析构和内存释放 两个过程 拆分成 1. 内存分配 2. 对象构造 3. 对象析构 4. 内存释放 四个过程，通过定制内存管理策略提高灵活性和效率</p>
</blockquote>
<ul>
<li>可以先进行内存分配（占位置），有数据时再进行对象构造，以避免对象构造（二次赋值&#x2F;拷贝）开销</li>
<li>可以先进行对象析构，不需要空间时再进行内存释放，以避免内存释放开销</li>
</ul>
<blockquote>
<p>参见：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/bfcf95a2a204">C++:allocator 学习整理 - 简书 (jianshu.com)</a></p>
</blockquote>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>C++ STL 六组件介绍。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li><p><a target="_blank" rel="noopener" href="https://c.biancheng.net/view/6593.html">C++ STL基本组成（6大组件+13个头文件） (biancheng.net)</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249/article/details/128418167">C++的基于对象编程范式、常用STL容器和C++11标准_基于对象的编程范式_夜悊的博客-CSDN博客</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5">cppreference.com</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/junxuezheng/article/details/129804072#:~:text=1%203%E7%A7%8D%E8%BF%AD%E4%BB%A3%E5%99%A8%E9%80%82%E9%85%8D%E5%99%A8%EF%BC%9A%E5%88%86%E5%88%AB%E6%98%AF%20%E5%8F%8D%E5%90%91%E8%BF%AD%E4%BB%A3%E5%99%A8%20%E9%80%82%E9%85%8D%E5%99%A8%E3%80%81%20%E6%8F%92%E5%85%A5%E5%9E%8B%E8%BF%AD%E4%BB%A3%E5%99%A8%20%E9%80%82%E9%85%8D%E5%99%A8%E3%80%81%20%E7%A7%BB%E5%8A%A8%E8%BF%AD%E4%BB%A3%E5%99%A8,%E9%80%82%E9%85%8D%E5%99%A8%E3%80%82%202%202%E7%A7%8D%E6%B5%81%E8%BF%AD%E4%BB%A3%E5%99%A8%EF%BC%9A%20%E6%B5%81%E8%BF%AD%E4%BB%A3%E5%99%A8%20%E3%80%81%20%E6%B5%81%E7%BC%93%E5%86%B2%E5%8C%BA%E8%BF%AD%E4%BB%A3%E5%99%A8%20%E3%80%81">【STL十三】适配器——迭代器适配器_郑同学的笔记的博客-CSDN博客</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/610255181">C++ STL：适配器 - 知乎 (zhihu.com)</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://c.biancheng.net/stl/iterator_adaptor/">C++ STL迭代器适配器完全攻略 (biancheng.net)</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/u014386899/article/details/108590804">为什么要用仿函数？_为什么要把函数写成仿函数-CSDN博客</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/bfcf95a2a204">C++:allocator 学习整理 - 简书 (jianshu.com)</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/99427686#:~:text=%E5%AE%9A%E4%BD%8D%20%28placement%29%20new%20%E8%BF%90%E7%AE%97%E7%AC%A6%20%E5%85%81%E8%AE%B8%E6%88%91%E4%BB%AC%E5%B0%86%20object%20%E6%88%96%E8%80%85%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%88%9B%E5%BB%BA%E5%9C%A8%E5%B7%B2%E7%94%B3%E8%AF%B7%E7%9A%84%E5%86%85%E5%AD%98%E4%B8%AD%EF%BC%8C%E5%B9%B6%E4%B8%94%20%E5%AE%9A%E4%BD%8D,%E5%AE%9A%E4%BD%8D%20%28placement%29%20delete%20%EF%BC%8C%E5%9B%A0%E4%B8%BA%20%E5%AE%9A%E4%BD%8D%20new%20%E8%BF%90%E7%AE%97%E7%AC%A6%20%E6%B2%A1%E6%9C%89%E7%94%B3%E8%AF%B7%E5%86%85%E5%AD%98%E7%A9%BA%E9%97%B4">C&#x2F;C++内存申请和释放(二) - 知乎 (zhihu.com)</a></p>
</li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/11/03/C++%20STL%20%E5%85%AD%E7%BB%84%E4%BB%B6%E4%BB%8B%E7%BB%8D/">http://example.com/2023/11/03/C++ STL 六组件介绍/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/C/">C++</a></div><div class="post_share"><div class="social-share" data-image="/img/cover12.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/10/25/%E7%8E%AF%E5%A2%83%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E2%80%9C%E5%8A%A0_%E5%8D%B8%E8%BD%BD%E2%80%9D%20Zend%20Framework%202/" title="环境编程代码实例：“加/卸载” Zend Framework 2"><img class="cover" src= "" data-lazy-src="/img/cover10.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">环境编程代码实例：“加/卸载” Zend Framework 2</div></div></a></div><div class="next-post pull-right"><a href="/2023/11/05/C++%20%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E5%A4%9A%E9%A1%B9%E5%BC%8F%E9%99%A4%E6%B3%95%E7%AE%80%E5%8D%95%E8%AE%A1%E7%AE%97%E5%B7%A5%E5%85%B7/" title="C++ 代码实例：多项式除法简单计算工具"><img class="cover" src= "" data-lazy-src="/img/cover17.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">C++ 代码实例：多项式除法简单计算工具</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/11/08/C%20%E5%92%8C%20C++%20%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E4%BB%8B%E7%BB%8D/" title="C 和 C++ 可变参数介绍"><img class="cover" src= "" data-lazy-src="/img/cover6.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-08</div><div class="title">C 和 C++ 可变参数介绍</div></div></a></div><div><a href="/2023/10/01/C++%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E6%8E%92%E5%88%97%E6%95%B0%E7%AE%80%E5%8D%95%E7%94%9F%E6%88%90%E5%B7%A5%E5%85%B7/" title="C++代码示例：排列数简单生成工具"><img class="cover" src= "" data-lazy-src="/img/cover15.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-01</div><div class="title">C++代码示例：排列数简单生成工具</div></div></a></div><div><a href="/2023/11/05/C++%20%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E5%A4%9A%E9%A1%B9%E5%BC%8F%E9%99%A4%E6%B3%95%E7%AE%80%E5%8D%95%E8%AE%A1%E7%AE%97%E5%B7%A5%E5%85%B7/" title="C++ 代码实例：多项式除法简单计算工具"><img class="cover" src= "" data-lazy-src="/img/cover17.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-05</div><div class="title">C++ 代码实例：多项式除法简单计算工具</div></div></a></div><div><a href="/2023/09/28/C++%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E7%BB%84%E5%90%88%E6%95%B0%E7%AE%80%E5%8D%95%E7%94%9F%E6%88%90%E5%B7%A5%E5%85%B7/" title="C++代码示例：组合数简单生成工具"><img class="cover" src= "" data-lazy-src="/img/cover6.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-09-28</div><div class="title">C++代码示例：组合数简单生成工具</div></div></a></div><div><a href="/2023/11/06/C++%20%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%AE%80%E5%8D%95%E5%88%9B%E5%BB%BA%E5%B7%A5%E5%85%B7/" title="C++ 代码实例：并查集简单创建工具"><img class="cover" src= "" data-lazy-src="/img/cover15.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-06</div><div class="title">C++ 代码实例：并查集简单创建工具</div></div></a></div><div><a href="/2023/10/02/C++%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E8%BF%9B%E5%88%B6%E6%95%B0%E7%AE%80%E5%8D%95%E7%94%9F%E6%88%90%E5%B7%A5%E5%85%B7/" title="C++代码示例：进制数简单生成工具"><img class="cover" src= "" data-lazy-src="/img/cover17.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-02</div><div class="title">C++代码示例：进制数简单生成工具</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5"><span class="toc-number">2.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%EF%BC%88container%EF%BC%89"><span class="toc-number">3.</span> <span class="toc-text">容器（container）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5-1"><span class="toc-number">3.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.</span> <span class="toc-text">类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-number">3.3.</span> <span class="toc-text">代码示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%EF%BC%88%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%EF%BC%89"><span class="toc-number">3.4.</span> <span class="toc-text">作用（为什么需要）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AE%97%E6%B3%95%EF%BC%88algorithm%EF%BC%89"><span class="toc-number">4.</span> <span class="toc-text">算法（algorithm）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5-2"><span class="toc-number">4.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B-1"><span class="toc-number">4.2.</span> <span class="toc-text">类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B-1"><span class="toc-number">4.3.</span> <span class="toc-text">代码示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%EF%BC%88%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%EF%BC%89-1"><span class="toc-number">4.4.</span> <span class="toc-text">作用（为什么需要）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8%EF%BC%88iterator%EF%BC%89"><span class="toc-number">5.</span> <span class="toc-text">迭代器（iterator）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5-3"><span class="toc-number">5.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">5.2.</span> <span class="toc-text">常用迭代器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E5%92%8C%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-number">5.3.</span> <span class="toc-text">类型和代码示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%AF%B9%E5%BA%94%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="toc-number">5.4.</span> <span class="toc-text">容器对应的迭代器的类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%EF%BC%88%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%EF%BC%89-2"><span class="toc-number">5.5.</span> <span class="toc-text">作用（为什么需要）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%EF%BC%8C%E7%AE%97%E6%B3%95%EF%BC%8C%E8%BF%AD%E4%BB%A3%E5%99%A8%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-number">6.</span> <span class="toc-text">容器，算法，迭代器代码示例</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%80%82%E9%85%8D%E5%99%A8%EF%BC%88adapter%EF%BC%89"><span class="toc-number">7.</span> <span class="toc-text">适配器（adapter）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5-4"><span class="toc-number">7.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E5%92%8C%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B-1"><span class="toc-number">7.2.</span> <span class="toc-text">类型和代码示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%EF%BC%88%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%EF%BC%89-3"><span class="toc-number">7.3.</span> <span class="toc-text">作用（为什么需要）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%BF%E5%87%BD%E6%95%B0%EF%BC%88functor%EF%BC%89"><span class="toc-number">8.</span> <span class="toc-text">仿函数（functor）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5-5"><span class="toc-number">8.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B-2"><span class="toc-number">8.2.</span> <span class="toc-text">代码示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%EF%BC%88%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%EF%BC%89-4"><span class="toc-number">8.3.</span> <span class="toc-text">作用（为什么需要）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%86%E9%85%8D%E5%99%A8%EF%BC%88allocator%EF%BC%89"><span class="toc-number">9.</span> <span class="toc-text">分配器（allocator）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5-6"><span class="toc-number">9.1.</span> <span class="toc-text">概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B-3"><span class="toc-number">9.2.</span> <span class="toc-text">代码示例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%EF%BC%88%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%EF%BC%89-5"><span class="toc-number">9.3.</span> <span class="toc-text">作用（为什么需要）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">10.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">11.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">12.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover12.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>